package cn.abcsys.cloud.devops.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.abcsys.cloud.devops.web.entity.GridBean;
import cn.abcsys.cloud.devops.web.entity.PagerModel;
import cn.abcsys.cloud.devops.web.service.ProjectService;

@Controller
@RequestMapping(value="/project")
public class ProjectController {
	
	@Resource
	private ProjectService projectService;
	
	private static final Logger LOGGER = Logger.getLogger(ProjectController.class);
	/**
	 * 分页查询当前环境的所有项目
	 * @param request
	 * @param map
	 * @return
	 */
/*	@RequestMapping(value="/getProjectByEnv",method={RequestMethod.POST})
	@ResponseBody
	public Result getProjectByEnv(HttpServletRequest request){
		
		List<Project> proList = null;
		Map<String,Integer> map = new HashMap<String,Integer>();
		Integer envId = (Integer)request.getSession().getAttribute("envId");
		if(null == envId)
			return new Result(false, "会话失效请重新登录");
		map.put("envId", envId);
		proList = projectService.getProjectByEnv(map);
		if(null == proList)
			return new Result(false, "无查询结果");
		return new Result(true, proList, "返回成功");
	}*/
	
	
	@RequestMapping(value = "/getProjectByEnv", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public GridBean getProjectByEnv(HttpServletRequest request, @RequestBody Map<String, Object> params){
		if(null == params){
			LOGGER.error("请求参数为空");
			return null;
		}else{
			try{
				JSONObject paramIn=new JSONObject(params);  //map转对象
				Map<String,Object> map = new HashMap<String,Object>();
				Integer envId = (Integer)request.getSession().getAttribute("envId");
				if(null == envId)
					return null;
				PagerModel pagerModel = paramIn.toJavaObject(PagerModel.class);
				map.put("envId", envId);
				return  projectService.getProjectByEnv(map,pagerModel);
			}catch(Exception e){
				LOGGER.error("请求参数转换错误："+e.getMessage());
				return null;
			}
		}
	}
	
}
